//
//  GSTabBar.swift
//  SinaWeiboDay1
//
//  Created by SamuelChan on 16/4/8.
//  Copyright © 2016年 SamuelChan. All rights reserved.
//

import UIKit

class GSTabBar: UITabBar {
    /**定义一个闭包属性,点击按钮的时候回调*/
    var callBack:(()->())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeButton)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    


    override func layoutSubviews() {
        super.layoutSubviews()
        let count : Int = GSTabBarButtonCount
        
        let screenSize : CGSize = bounds.size
        let width = screenSize.width / CGFloat(count)
        
        var index = 0
        for subView in subviews where   subView.isKindOfClass(NSClassFromString("UITabBarButton")!)
        {
            subView.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: 46)
            index++
            
            if index == 2 {
                index++
            }
        }
        /**设置加号的位置*/
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: 46)

    }
    
    //MARK: - 自定义的函数
    func addButtonDidClick (){
        //如果外面实现了就进行回调
        callBack?()
    }
    
    //MARK: - 懒加载
    lazy var composeButton : UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
    
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.addTarget(self, action: Selector("addButtonDidClick"), forControlEvents: .TouchUpInside)
        return button
        
    }()
    
    
}
